import { Component, Input, OnDestroy, OnInit } from '@angular/core';
import { ApiService } from '@core/services/api';
import { _HttpClient } from '@delon/theme';
import { SPage } from '@shared/components/table-page/table-page.component';
import { UtilService } from '@core/services/util.service';
import { MartialStatus } from '@core/config/dic.config';

@Component({
  selector: 'app-inout-detail',
  templateUrl: './inout-detail.component.html',
  styleUrls: ['./inout-detail.component.less']
})
export class InoutDetailComponent implements OnInit, OnDestroy {
  @Input()
  set param(value) {
    this.recordDetail = value;
    this.recordDetail.openTime = value.createTime;
    // this.getData({peopleId: 'f9a4a98ce0ce4a7cb55b58540f51f563'});
    this.getData({peopleId: value.peopleId});
  }
  recordDetail: any;
  activeIndex = 0;
  timer: any;
  data: any = {};
  manager: any;
  doorCard: any = {};
  itemArray: Array<{title: string, index, items?}>= [
    {title: '房屋地址', index: 'houseAddr'},
    {title: '房屋编码', index: 'houseCode'},
    {title: '房屋类型', index: 'houseType', items: {}},
    {title: '层数', index: 'floors'},
    {title: '房间数', index: 'rooms'},
    {title: '开门方式', index: 'type', items: {
      1: '人脸识别',
      2: '可视对讲',
      3: 'APP',
      4: '智能门卡',
      }},
    {title: '开门时间', index: 'openTime'},
    {title: '姓名', index: 'infoName'},
    {title: '性别', index: 'infoSex', items: {
      1: '男',
      2: '女',
      }},
    {title: '民族', index: 'infoNation'},
    {title: '婚姻状况', index: 'infoMaritalStatus', /*items: this.util.getObjDic(MartialStatus)*/},
    {title: '联系方式', index: 'infoMobile'},
    {title: '证件有效期起', index: 'infoValidPeriod'},
    {title: '证件有效期止', index: 'infoValidTill'},
    {title: '证件类型', index: 'infoIdcardType', items: {
      1: '身份证',
        2: '驾驶证'
      }},
    {title: '证件号码', index: 'infoIdcardNumber'},
    {title: '证件地址', index: 'infoNativeAddress'},
    {title: '入住时间', index: 'roomInTime'},
  ];
  imgArray = ['./assets/images/1.jpg', './assets/images/2.jpg', './assets/images/3.jpg', './assets/images/4.jpg'];
  doorCardInfo: Array<{title: string, index, items?}> = [
    {title: '门卡状态', index: 'status', items: {
      0: '无效',
        1: '有效',
        2: '丢失',
        3: '损坏',
        4: '冻结',
        5: '其他'
      }},
    {title: '有效时间', index: 'RelevantTime'},
    {title: '门卡类型', index: 'doorcardBusiType', items: {
      1: "IC卡",
      2: "CPU卡",
      3: "身份证",
      4: "居住证",
      5: "银行卡",
      }},
    {title: '门卡物理号', index: 'doorcardCode'},
    {title: '办卡时间', index: 'doorcardRelevantTime'},

  ]
  // 管理员设置
  config: SPage = {
    columns: [
      {
        index: 'managerName',
        title: '姓名',
        filter: {
          placeholder: '',
          type: 'string',
        },
      },
      {
        index: 'type',
        title: '管理员类型',
        filter: {
          placeholder: '请选择管理员类型',
          type: 'select',
          items: [
            { label: '业主', value: '1' },
            { label: '综管员', value: '2' },
            { label: '房屋管理员', value: '3' },
            { label: '运维人员', value: '4' },
            { label: '清洁人员', value: '5' },
          ],
        },
      },
      {
        index: 'infoMobile',
        title: '手机号码',
        filter: {
          placeholder: '请输入手机号',
          type: 'string',
        },
      }
    ],
    hideSearch: true,
    data: []
  }
  constructor(
    private api: ApiService,
    private http: _HttpClient,
    private util: UtilService
  ) { }

  ngOnInit() {
    this.autoPlay();
  }
  ngOnDestroy(): void {
    clearInterval(this.timer);
  }

  getData(body: {peopleId}) {
    this.http.request<any>(this.api.inout.detail.method, this.api.inout.detail.url, {body}).subscribe( res => {
      if (res.status === '200') {
        this.data = res.data.peopleInfo ?  {...this.recordDetail, ...res.data.peopleInfo} : this.recordDetail;
        console.log(this.data)
        this.manager = res.data.manager || {};
        this.doorCard = res.data.doorCard;
        this.config.data = res.data.manager;
      }
    })
  }
  imgPlay(index): void {
    this.activeIndex = index;
  }
  autoPlay(): void {
    clearInterval(this.timer);
    if (this.imgArray.length <= 1) return;
    this.timer = setInterval(() => {
      this.activeIndex = ++this.activeIndex % this.imgArray.length;
    }, 5000)
  }
  stop(): void {
    clearInterval(this.timer);
  }
}
